//
//  SLCommonsSecretRoomMessageBuilder.m
//  SecretRoom
//
//  Created by Sitdhibhong Laokok on 9/12/54 BE.
//  Copyright 2554 App Tree. All rights reserved.
//

#import "ASIHTTPRequest.h"

#import "SLCommonsSecretRoomMessageBuilder.h"
#import "SLCommonsDateUtility.h"
#import "SLCommonsMapsDisplay.h"

#import "Event.h"
#import "Evidence.h"
#import "EvidenceMeta.h"

#import "NSData+MD5.h"

@implementation SLCommonsSecretRoomMessageBuilder

@synthesize event = _event;

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (id)initWithEvent:(Event *)initEvent
{
    if (self = [super init]) {
        self.event = initEvent;
    }
    
    return self;
}

- (NSString *)messageBody
{
    if (_event == nil) {
        return nil;
    }

    NSMutableString *emailBody = [[NSMutableString alloc] initWithString:@"<html><body>"];
    
    [emailBody appendFormat:@"<p><strong>%@</strong></p>", self.event.eventName];
    [emailBody appendFormat:@"<p>Note: <strong>%@</strong></p>", self.event.note];
    [emailBody appendFormat:@"<p><a href=\"%@\"><img src=\"%@\" width=\"300\" height=\"300\" /></a><p>", 
     [SLCommonsMapsDisplay googleMapsQueryStringURLWithLocation:CLLocationCoordinate2DMake([[NSString stringWithFormat:@"%@", self.event.latitude] floatValue], [[NSString stringWithFormat:@"%@", self.event.longitude] floatValue]) displayWithZoomLevel:16],
     [SLCommonsMapsDisplay urlMapsWithLocation:CLLocationCoordinate2DMake([[NSString stringWithFormat:@"%@", self.event.latitude] floatValue], [[NSString stringWithFormat:@"%@", self.event.longitude] floatValue]) displayWithZoomLevel:16 size:CGSizeMake(300, 300)]];
    
    [emailBody appendFormat:@"<p>datetime: %@</p>", [SLCommonsDateUtility GMTDateToLocalDate:self.event.eventTimestamp]];
    
    if ([self.event.evidences count] > 0) {
        int count = 1;
        
        for (Evidence *attatchedEvidence in self.event.evidences) {
            [emailBody appendFormat:@"<p>%i. %@", count++, [self evidenceNameResolverFromEvidence:attatchedEvidence]];
            [emailBody appendFormat:@"<ul><li>file name: <strong>%@</strong></li>", [[attatchedEvidence localUrl] lastPathComponent]];
            
            if ([[NSUserDefaults standardUserDefaults] boolForKey:kSRPreferenceShowEvidenceCollectedTimestamp]) {
                [emailBody appendFormat:@"<li>timestamp: <strong>%@</strong></li>", [SLCommonsDateUtility GMTDateToLocalDate:attatchedEvidence.datetimeCollected]];  
            }
            
            if ([[NSUserDefaults standardUserDefaults] boolForKey:kSRPreferenceShowEvidenceHash]) {
                EvidenceMeta *evidenceMeta = [[[attatchedEvidence metadatas] allObjects] lastObject];
                if ([evidenceMeta.metaName isEqual:kSRImageMetadataHash]) {
                    [emailBody appendFormat:@"<li>Hash (MD5): <strong>%@</strong></li>", evidenceMeta.metaValue];
                }
            }
            
            [emailBody appendString:@"</ul></p><br />"];
            
        }
        
    }
    
    [emailBody appendString:@"</body></html>"];
    
    NSLog(@"email body: %@", emailBody);
    
    return [emailBody autorelease];
}

- (NSString *)messageForEvent:(Event *)evt
{
    self.event = evt;
    
    return [self messageBody];
}

- (NSString *)evidenceNameResolverFromEvidence:(Evidence *)evidence
{
    return (evidence.evidenceName != nil) ? evidence.evidenceName : [[SLCommonsDateUtility localDefaultDateTimeFormat:SLCommonsDateUtilityDateTimeFormatShortFormat] stringFromDate:[SLCommonsDateUtility GMTDateToLocalDate:evidence.datetimeCollected]];
}

@end
